package com.chendx.article.stream.listener;

import com.alibaba.fastjson.JSON;
import com.chendx.article.config.stream.KafkaStreamListener;
import com.chendx.common.message.HotArticleConstants;
import com.chendx.model.article.mess.ArticleVisitStreamMess;
import com.chendx.model.mess.app.UpdateArticleMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.kstream.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * Created by chendx on 2022/3/18.
 */
@Component
@Slf4j
public class HotArticleStreamHandler implements KafkaStreamListener<KStream<String,String>> {
    @Override
    public String listenerTopic() {
        System.out.println("listenerTopic");
        return HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC;
    }

    @Override
    public String sendTopic() {
        System.out.println("sendTopic");
        return HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC;
    }

    @Override
    public KStream<String, String> getService(KStream<String, String> stream) {
        return stream.flatMapValues(new ValueMapper<String, Iterable<String>>() {
            @Override
            public Iterable<String> apply(String value) {
                UpdateArticleMess updateArticleMess = JSON.parseObject(value, UpdateArticleMess.class);
                log.info("recevie message value:{}",updateArticleMess);
                System.out.println(updateArticleMess);
                //按照文章的id和行为进行聚合查询  --》  1232323232323:LIEKS
                return Arrays.asList(updateArticleMess.getArticleId()+":"+updateArticleMess.getType());
            }
        }).map(new KeyValueMapper<String, String, KeyValue<String, String>>() {
            @Override
            public KeyValue<String, String> apply(String key, String value) {

                System.out.println(key +" "+value);
                return new KeyValue<String, String>(value,value);
            }
        })
                .groupByKey()
                .windowedBy(TimeWindows.of(10000))
                .count(Materialized.as("count-article-num-00008"))
                .toStream()
                .map((key,value)->{
                    //key.key() =>1232323232323:LIEKS
                    //value => count()

                    System.out.println(key +" map "+value);
                    ArticleVisitStreamMess mess = new ArticleVisitStreamMess();
                    String mapKey = key.key();
                    String[] split = mapKey.split(":");
                    mess.setArticleId(Long.valueOf(split[0]));
                    if(split[1].equals(UpdateArticleMess.UpdateArticleType.LIKES.name())){
                        mess.setLike(Long.valueOf(value));
                    }
                    if(split[1].equals(UpdateArticleMess.UpdateArticleType.VIEWS.name())){
                        mess.setView(Long.valueOf(value));
                    }
                    if(split[1].equals(UpdateArticleMess.UpdateArticleType.COLLECTION.name())){
                        mess.setCollect(Long.valueOf(value));
                    }
                    if(split[1].equals(UpdateArticleMess.UpdateArticleType.COMMENT.name())){
                        mess.setComment(Long.valueOf(value));
                    }
                    log.info("处理之后的结果为:{}",mess);
                    System.out.println("处理之后的结果为 " + mess);
                    return new KeyValue<>(key.key(),JSON.toJSONString(mess));
                });
    }
}
